﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VLUSM_DataAccess.Repositories
{
    public class LoaiHoSoRepository : GenericRepository<MainDataContainer>
    {
        public LoaiHoSoRepository(string connectionString) : base(connectionString, "MainData") { }
        public LoaiHoSoRepository(MainDataContainer context) : base(context) { }

        public LoaiHoSo GetSingleByID(int IDFilter)
        {
            var query = this.GetFirst<LoaiHoSo>(d => d.ID == IDFilter);
            return query;
        }

        public LoaiHoSo GetSingleByTen(string TenFilter)
        {
            var query = this.GetFirst<LoaiHoSo>(d => d.TenGoi == TenFilter);
            return query;
        }

        public IEnumerable<LoaiHoSo> GetAll()
        {
            var query = this.GetAll<LoaiHoSo>();
            return query;
        }

        public IEnumerable<LoaiHoSo> GetAllByDonViID(int DonViIDFilter)
        {
            var query = this.Find<LoaiHoSo>(h => this.Any<DonVi>(d => d.ID == DonViIDFilter));
            return query;
        }

        public void Save(LoaiHoSo obj)
        {
            try
            {
                if (obj.ID != 0)
                {
                    this.Attach<LoaiHoSo>(obj);
                }
                else
                {
                    this.Add<LoaiHoSo>(obj);
                }
            }
            catch { }
        }

        public void Remove(LoaiHoSo obj)
        {
            this.Delete<LoaiHoSo>(obj);
        }

        public void Remove(int IDFilter)
        {
            var obj = this.GetSingleOrDefault<LoaiHoSo>(h => h.ID == IDFilter);
            this.Delete<LoaiHoSo>(obj);
        }
    }
}

